package com.youlu.campus.admin.course.service.impl;

import com.youlu.campus.admin.course.convert.CourseConvert;
import com.youlu.campus.admin.course.dto.CourseVideoOpDTO;
import com.youlu.campus.admin.course.dto.CourseVideoSaveDTO;
import com.youlu.campus.admin.course.dto.CourseVideoSearchDTO;
import com.youlu.campus.admin.course.service.CourseVideoService;
import com.youlu.campus.base.exception.BusinessException;
import com.youlu.campus.entity.course.ActivityCourse;
import com.youlu.campus.entity.course.ActivityCourseVideo;
import com.youlu.campus.service.course.QueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.repository.support.PageableExecutionUtils;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author : [dengbo]
 * @className : CourseVideoServiceImpl
 * @description : [课程视频类]
 * @createTime : [2022/5/16 23:17]
 */
@Service
@Slf4j
public class CourseVideoServiceImpl implements CourseVideoService {

    @Autowired
    private MongoTemplate mongoTemplate;

    @Autowired
    private CourseConvert courseConvert;

    @Override
    public boolean save(CourseVideoSaveDTO saveDTO) {
        ActivityCourseVideo video = null;
        if (StringUtils.isNotBlank(saveDTO.getId())) {
            //修改
            video = mongoTemplate.findOne(Query.query(Criteria.where("id").is(saveDTO.getId())), ActivityCourseVideo.class);
            if (video == null) {
                throw new BusinessException("修改音视频信息不存在");
            }
        }
        ActivityCourse activityCourse = mongoTemplate.findOne(Query.query(Criteria.where("id").is(saveDTO.getCourseId())), ActivityCourse.class);
        if (activityCourse == null) {
            throw new BusinessException("课程信息不存在");
        }
        video = courseConvert.courseVideoSaveDTOToEntity(saveDTO, video);
        mongoTemplate.save(video);
        return true;
    }

    @Override
    public Page<ActivityCourseVideo> list(CourseVideoSearchDTO dto) {
        QueryWrapper query = new QueryWrapper();
        if (StringUtils.isNotBlank(dto.getCourseId())) {
            query.addCriteria(Criteria.where("courseId").is(dto.getCourseId()));
        }
        if (StringUtils.isNotBlank(dto.getName())) {
            query.like("name", dto.getName());
        }
        query.addCriteria(Criteria.where("deleted").ne(true));
        long count = mongoTemplate.count(query, ActivityCourseVideo.class);
        query.with(Sort.by(Sort.Order.desc("createdTime")));
        PageRequest pageRequest = PageRequest.of(dto.getPage() - 1, dto.getPageSize());
        List<ActivityCourseVideo> data = mongoTemplate.find(query.with(pageRequest), ActivityCourseVideo.class);
        return PageableExecutionUtils.getPage(data, pageRequest, () -> count);
    }

    @Override
    public boolean operation(CourseVideoOpDTO saveDTO) {
        ActivityCourseVideo video = mongoTemplate.findOne(Query.query(Criteria.where("id").is(saveDTO.getVideoId())), ActivityCourseVideo.class);
        if (video == null) {
            throw new BusinessException("音视频信息不存在");
        }
        int valid = video.getValid() == 0 ? 1 : 0;
        video.setValid(valid);
        mongoTemplate.save(video);
        return true;
    }
}
